package com.sskj.market.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.sskj.common.base.App;
import com.sskj.market.R;

/**
 * @author :budou
 * Package :com.sskj.market.view
 * Date :2019/7/31 18:02
 * Describe :商城添加减少数据
 * Remark :
 */
public class MarketMoreAndLessView extends LinearLayout {
    private boolean lessEnable = false;
    private boolean moreEnable = true;
    private int maxCount = 999;

    private LessAndMoreCallBack lessAndMoreCallBack;
    private ImageView spDelete;
    private ImageView spAdd;
    private EditText etCurrent;
    private int currentValue;

    public MarketMoreAndLessView(Context context) {
        this(context, null);
    }

    public MarketMoreAndLessView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MarketMoreAndLessView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initContext(context, attrs, defStyleAttr);
    }

    private void initContext(Context context, AttributeSet attrs, int def) {
        @SuppressLint("Recycle")
        TypedArray typedArray = (TypedArray) context.obtainStyledAttributes(attrs, R.styleable.MarketMoreAndLessView);
        maxCount = typedArray.getInt(R.styleable.MarketMoreAndLessView_market_max_count, 999);
        lessEnable = typedArray.getBoolean(R.styleable.MarketMoreAndLessView_market_less_enable, false);
        moreEnable = typedArray.getBoolean(R.styleable.MarketMoreAndLessView_market_more_enable, true);
        typedArray.recycle();
        init(context);
    }

    public boolean isLessEnable() {
        return lessEnable;
    }

    public void setLessEnable(boolean lessEnable) {
        this.lessEnable = lessEnable;
    }

    public boolean isMoreEnable() {
        return moreEnable;
    }

    public void setMoreEnable(boolean moreEnable) {
        this.moreEnable = moreEnable;
    }

    public int getMaxCount() {
        return maxCount;
    }

    public void setMaxCount(int maxCount) {
        this.maxCount = maxCount;
    }

    private void init(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.market_more_less_view, this);

        spDelete = view.findViewById(R.id.iv_less_market);
        spAdd = view.findViewById(R.id.iv_more_market);
        etCurrent = view.findViewById(R.id.et_input_market);
        etCurrent.clearFocus();
        spAdd.setEnabled(moreEnable);
        spDelete.setEnabled(lessEnable);
        spAdd.setOnClickListener(v -> {
            currentValue = Integer.parseInt(etCurrent.getText().toString());
            currentValue++;
            lessAndMoreCallBack.addCallBack(currentValue);
            setValue(currentValue);
            if (currentValue >= maxCount) {
                spAdd.setEnabled(false);
                lessAndMoreCallBack.outOfValueCallBack(App.INSTANCE.getString(R.string.market_marketMoreAndLessView1));
            }
            if (!spDelete.isEnabled()) {
                spDelete.setEnabled(true);
            }
        });

        spDelete.setOnClickListener(v -> {
            currentValue = Integer.parseInt(etCurrent.getText().toString());
            currentValue--;
            lessAndMoreCallBack.deleteCallBack(currentValue);
            setValue(currentValue);
            if (currentValue <= 1) {
                spDelete.setEnabled(false);
                setValue(0);
                lessAndMoreCallBack.outOfValueCallBack(App.INSTANCE.getString(R.string.market_marketMoreAndLessView1));
            }
            if (!spAdd.isEnabled()) {
                spAdd.setEnabled(true);
            }
        });


    }

    public int getCurrentValue() {
        return Integer.parseInt(etCurrent.getText().toString());
    }


    /**
     * 对参数赋值
     *
     * @param value
     */
    public void setValue(int value) {
        try {
            if (lessAndMoreCallBack != null) {
                if (value > maxCount) {
                    //将最大值赋值
                    etCurrent.setText(String.valueOf(maxCount));
                    lessAndMoreCallBack.outOfValueCallBack(App.INSTANCE.getString(R.string.market_marketMoreAndLessView2));
                    lessAndMoreCallBack.valueChangeCallBack(maxCount);
                } else {
                    if (value == 1 || value == 0) {
                        spDelete.setEnabled(false);
                    }
                    etCurrent.setText(String.valueOf(value));
                    lessAndMoreCallBack.valueChangeCallBack(value);
                }
            } else {
                throw new Exception(App.INSTANCE.getString(R.string.market_marketMoreAndLessView3));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public EditText getEtCurrent() {
        return etCurrent;
    }

    /**
     * 添加 减少 相关回调
     */
    public interface LessAndMoreCallBack {
        /**
         * 选中增加提示
         *
         * @param current 当前选中数值
         */
        void addCallBack(int current);

        /**
         * 减少库存提示
         *
         * @param current 当前选中
         */
        void deleteCallBack(int current);

        /**
         * 超出最大库存提示
         *
         * @param msg 超出最大限度回调提示语
         */
        void outOfValueCallBack(String msg);

        /**
         * 每次选择提示
         *
         * @param current 当前选中值
         */
        void valueChangeCallBack(int current);
    }

    public void setLessAndMoreCallBack(LessAndMoreCallBack lessAndMoreCallBack) {
        this.lessAndMoreCallBack = lessAndMoreCallBack;
    }
}
